
/**************************************************START OF FILE*****************************************************/




/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "widget.h"
#include "ui_widget.h"
#include "Tool.h"
#include <QRegExpValidator>

#include <QImage>


/*
*********************************************************************************************************************

@ Brief  : 构造函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:04:27

*********************************************************************************************************************
*/
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{

    LearnModel = 0;
    ui->setupUi(this);

    //加载要用到的图片
    img_led_r = new QImage(":/LED/img/LED_R.png");
    img_led_g = new QImage(":/LED/img/LED_G.png");
    img_led_y = new QImage(":/LED/img/LED_B.png");



    //设备连接状态
    ui->LB_ConnectStatus->setPixmap(QPixmap::fromImage(*img_led_r));
    ui->LB_ConnectStatus->setScaledContents(true);
    ui->LB_ConnectStatusT->setText("设备已断开连接");

    //输出终端
    ui->TE_OutputMsg->setReadOnly(true);

    //学习模式选择框
    btnGroupLearn = new QButtonGroup(this);
    btnGroupLearn->addButton(ui->RB_Simple , 0);
    btnGroupLearn->addButton(ui->RB_Standard , 1);
    btnGroupLearn->addButton(ui->RB_Safe , 2);
    ui->RB_Simple->setChecked(true);
    QObject::connect(ui->RB_Simple, &QRadioButton::clicked , this , &Widget::LearnModeSelect);
    QObject::connect(ui->RB_Standard, &QRadioButton::clicked , this , &Widget::LearnModeSelect);
    QObject::connect(ui->RB_Safe, &QRadioButton::clicked , this , &Widget::LearnModeSelect);


    //创建线程
    UsbDeviceWork = new UsbDeviceWorker();
    UsbDeviceWork->moveToThread(&UsbDeviceThread);
    connect(this, &Widget::operate, UsbDeviceWork, &UsbDeviceWorker::DoWork);
    UsbDeviceThread.start();
    emit operate("aa");


    //线程通信的信号
    connect(UsbDeviceWork, &UsbDeviceWorker::PostMessage, this, &Widget::ReceiveThreadMsg);
    connect(this, &Widget::SendMsgToUsbThread, UsbDeviceWork, &UsbDeviceWorker::RxMessage);


    //两个输入的文本框
    QRegExp product_rx("[0-9A-Fa-f]{0,16}");
    QRegExpValidator *validator = new QRegExpValidator(product_rx, this);
    ui->LE_Keyt->setValidator(validator);

    QRegExp IdCode_rx("[0-9A-Fa-f]{0,3}");
    validator = new QRegExpValidator(IdCode_rx, this);
    ui->LE_IdCode->setValidator(validator);



}


/*
*********************************************************************************************************************

@ Brief  : 析构函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:05:03

*********************************************************************************************************************
*/
Widget::~Widget()
{

    delete img_led_r;
    delete img_led_g;
    delete img_led_y;


    delete ui;
}




/*
*********************************************************************************************************************

@ Brief  : 输出到终端的信息处理

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:05:28

*********************************************************************************************************************
*/
void Widget::OutputMessage(QString str)
{
    static uint32_t Line = 0;
    Line++;
    if(Line > 200)
    {
        ui->TE_OutputMsg->clear();
        Line = 1;
    }
    QString str1 = QString("%1:").arg(Line , 3 , 10 , QLatin1Char('0'));
    str = str1 + str;
    ui->TE_OutputMsg->append(str);
    ui->TE_OutputMsg->moveCursor(QTextCursor::End);
}



/*
*********************************************************************************************************************

@ Brief  : 学习模式改变事件

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:06:01

*********************************************************************************************************************
*/
void Widget::LearnModeSelect(void)
{
    switch(btnGroupLearn->checkedId())
    {
        //简单
        case 0:
            LearnModel = 0;
        break;
        //标准
        case 1:
            LearnModel = 1;
        break;
        //安全
        case 2:
            LearnModel = 2;
        break;
    }
}



/*
*********************************************************************************************************************

@ Brief  : 读取配置信息按键

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:06:27

*********************************************************************************************************************
*/
void Widget::on_BT_ReadPara_clicked()
{
    uint16_t CheckSum = 0;
    uint8_t* TxBuf;

    TxBuf = (uint8_t*)malloc(64);
    TxBuf[0] = 0xAA;
    TxBuf[1] = 0x55;
    TxBuf[62] = 0x55;
    TxBuf[63] = 0xAA;
    TxBuf[2] = 0xF0;


    //CHECKSUM计算
    for (uint8_t  i = 0; i < 58; i++)
    {
        CheckSum += TxBuf[2+i];
    }
    //CHECKSUM
    TxBuf[60] = (uint8_t)(CheckSum>>8);
    TxBuf[61] = (uint8_t)(CheckSum>>0);

    emit SendMsgToUsbThread(TxBuf);

    OutputMessage("读取配置信息");
}


/*
*********************************************************************************************************************

@ Brief  : 写入配置按键事件

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:00:49

*********************************************************************************************************************
*/
void Widget::on_BT_WritePara_clicked()
{
    uint16_t CheckSum = 0;
    uint8_t* TxBuf;
    QString str;
    uint32_t temp;
    uint64_t keyt;
    uint32_t idcode;
    bool ok;
    str = ui->LE_Keyt->text();
    keyt = str.toULongLong(&ok , 16);
    str = ui->LE_IdCode->text();
    idcode = str.toUInt(&ok , 16);

    TxBuf = (uint8_t*)malloc(64);
    TxBuf[0] = 0xAA;
    TxBuf[1] = 0x55;
    TxBuf[62] = 0x55;
    TxBuf[63] = 0xAA;
    TxBuf[2] = 0xF1;
    temp = keyt;
    TOOL_Uint32ToUint8_H(&TxBuf[3] ,temp);
    temp = keyt>>32;
    TOOL_Uint32ToUint8_H(&TxBuf[7] ,temp);
    temp = idcode;
    TOOL_Uint32ToUint8_H(&TxBuf[11] ,temp);

    TxBuf[15] = LearnModel;

    //CHECKSUM计算
    for (uint8_t  i = 0; i < 58; i++)
    {
        CheckSum += TxBuf[2+i];
    }
    //CHECKSUM
    TxBuf[60] = (uint8_t)(CheckSum>>8);
    TxBuf[61] = (uint8_t)(CheckSum>>0);

    emit SendMsgToUsbThread(TxBuf);

    OutputMessage("写入配置信息");

}


/*
*********************************************************************************************************************

@ Brief  : 从子线程接收到的消息

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 22:07:07

*********************************************************************************************************************
*/
void Widget::ReceiveThreadMsg(uint8_t* Msg)
{
    if((Msg[0] == 0xAA) && (Msg[1] == 0x55))
    {
        switch (Msg[2])
        {
            //设备连接消息
            case 0xF1:
                if(Msg[3])
                {
                    ui->LB_ConnectStatus->setPixmap(QPixmap::fromImage(*img_led_g));
                    ui->LB_ConnectStatusT->setText("设备已经连接");
                    OutputMessage("设备连接成功");
                }
                else
                {
                    ui->LB_ConnectStatus->setPixmap(QPixmap::fromImage(*img_led_r));
                    ui->LB_ConnectStatusT->setText("设备断开连接");
                    OutputMessage("设备断开连接");
                }

            break;

            case 0xE0:
                {
                uint32_t Rxdata[2];
                Rxdata[0] = TOOL_Uint8ToUint32_L((uint8_t*)(&Msg[3]));
                Rxdata[1] = TOOL_Uint8ToUint32_L((uint8_t*)(&Msg[7]));
                QString str = QString("%1%2").arg(Rxdata[0] , 8 , 16 , QLatin1Char('0')).arg(Rxdata[1] , 8 , 16 , QLatin1Char('0')).toUpper();
                ui->LE_Keyt->setText(str);

                }
            break;

            case 0xF0:
                uint32_t Rxdata[2];
                Rxdata[0] = TOOL_Uint8ToUint32_L((uint8_t*)(&Msg[3]));
                Rxdata[1] = TOOL_Uint8ToUint32_L((uint8_t*)(&Msg[7]));
                QString str("RF接收到数据: ");
                QString strdata1 = QString("%1").arg(Rxdata[0],8,16,QLatin1Char('0')).toUpper();
                QString strdata2 = QString("%1").arg(Rxdata[1],8,16,QLatin1Char('0')).toUpper();
                str = str + QString("Enc:0x") + strdata1 + QString(" Fix:0x") + strdata2;
                OutputMessage(str);

                ui->LE_EncryptPor->setText(QString("0x")+strdata1);
                ui->LE_FixedPor->setText(QString("0x")+strdata2);
            break;
        }
    }
    free(Msg);
}

/**************************************************END OF FILE**********************************************************/

